// 文档： https://blog.csdn.net/u011054333/article/details/60779700

def template(){
    def text = '<h1>${title}</h1><data>${payload}</data>' 

    def binding = ["title":"My Title", "payload":"Hello world"] 

    def engine = new groovy.text.SimpleTemplateEngine() 
    def template = engine.createTemplate(text).make(binding) 
    println template.toString() // <h1>My Title</h1><data>Hello world</data>     
}

def template2(){

    def emailTemplate = '''
        <div id="mailContentContainer" class="qmbox qm_con_body_content qqmail_webmail_only" style="">
            <div>
                <font face="宋体" size="3">

                    <span style="font-family: 宋体; mso-ascii-font-family: &quot;Times New Roman&quot;; mso-hansi-font-family: &quot;Times New Roman&quot;;">
                        <font size="3">您好：</font>
                    </span>
                </font>
                <font face="宋体" size="3">

                </font>
                <span style="line-height: 150%; font-family: 宋体; font-size: 12pt; mso-ascii-font-family: &quot;Times New Roman&quot;; mso-hansi-font-family: &quot;Times New Roman&quot;; mso-bidi-font-family: &quot;Times New Roman&quot;; mso-font-kerning: 1.0pt; mso-ansi-language: EN-US; mso-fareast-language: ZH-CN; mso-bidi-language: AR-SA;">&nbsp;&nbsp; </span>
            </div>
            <div>
                <span style="line-height: 150%; font-family: 宋体; font-size: 12pt; mso-ascii-font-family: &quot;Times New Roman&quot;; mso-hansi-font-family: &quot;Times New Roman&quot;; mso-bidi-font-family: &quot;Times New Roman&quot;; mso-font-kerning: 1.0pt; mso-ansi-language: EN-US; mso-fareast-language: ZH-CN; mso-bidi-language: AR-SA;">&nbsp; &nbsp; 本次执行的测试为：${TEST_SUITE_NAME}</span>
                <span style="line-height: 150%; font-family: 宋体; font-size: 12pt; mso-ascii-font-family: &quot;Times New Roman&quot;; mso-hansi-font-family: &quot;Times New Roman&quot;; mso-bidi-font-family: &quot;Times New Roman&quot;; mso-font-kerning: 1.0pt; mso-ansi-language: EN-US; mso-fareast-language: ZH-CN; mso-bidi-language: AR-SA;">，测试用例通过${PASS_COUNT}</span>
                <span style="line-height: 150%; font-family: 宋体; font-size: 12pt; mso-ascii-font-family: &quot;Times New Roman&quot;; mso-hansi-font-family: &quot;Times New Roman&quot;; mso-bidi-font-family: &quot;Times New Roman&quot;; mso-font-kerning: 1.0pt; mso-ansi-language: EN-US; mso-fareast-language: ZH-CN; mso-bidi-language: AR-SA;">个，不通过${NO_PASS_COUNT}</span>
                <span style="line-height: 150%; font-family: 宋体; font-size: 12pt; mso-ascii-font-family: &quot;Times New Roman&quot;; mso-hansi-font-family: &quot;Times New Roman&quot;; mso-bidi-font-family: &quot;Times New Roman&quot;; mso-font-kerning: 1.0pt; mso-ansi-language: EN-US; mso-fareast-language: ZH-CN; mso-bidi-language: AR-SA;">个，通过率${PASS_RATIO}</span>
                <span lang="EN-US" style="line-height: 150%; font-family: &quot;Times New Roman&quot;,&quot;serif&quot;; font-size: 12pt; mso-font-kerning: 1.0pt; mso-ansi-language: EN-US; mso-fareast-language: ZH-CN; mso-bidi-language: AR-SA; mso-fareast-font-family: 宋体;">%</span>
                <span style="line-height: 150%; font-family: 宋体; font-size: 12pt; mso-ascii-font-family: &quot;Times New Roman&quot;; mso-hansi-font-family: &quot;Times New Roman&quot;; mso-bidi-font-family: &quot;Times New Roman&quot;; mso-font-kerning: 1.0pt; mso-ansi-language: EN-US; mso-fareast-language: ZH-CN; mso-bidi-language: AR-SA;">。</span>
            </div>
            <div>
                <span style="line-height: 150%; font-family: 宋体; font-size: 12pt; mso-ascii-font-family: &quot;Times New Roman&quot;; mso-hansi-font-family: &quot;Times New Roman&quot;; mso-bidi-font-family: &quot;Times New Roman&quot;; mso-font-kerning: 1.0pt; mso-ansi-language: EN-US; mso-fareast-language: ZH-CN; mso-bidi-language: AR-SA;">&nbsp; &nbsp; <span style="line-height: 150%; font-family: 宋体; font-size: 12pt; mso-ascii-font-family: &quot;Times New Roman&quot;; mso-hansi-font-family: &quot;Times New Roman&quot;; mso-bidi-font-family: &quot;Times New Roman&quot;; mso-font-kerning: 1.0pt; mso-ansi-language: EN-US; mso-fareast-language: ZH-CN; mso-bidi-language: AR-SA; mso-no-proof: yes;">测试结果详情：</span>
                </span>
            </div>
            <div>
                
            </div>					
                        ''';
                        
    def binding = ["TEST_SUITE_NAME":"LQ-TEST_SUITE_NAME", "PASS_COUNT":"3", "NO_PASS_COUNT":"2","PASS_RATIO":"50%"] 

    def engine = new groovy.text.SimpleTemplateEngine() 
    def template = engine.createTemplate(emailTemplate).make(binding)
    println template

}

def template_foreach(){
    def f = '''
- env:<% env.each { v -> %>
- name : ${v.name}
    value : ${v.value}<% } %>
project: "${PROJECT}"
ports:<% ports.each { v -> %>
- lq : ${v.port == 1000 ? '1001':'10002'}
- port: ${v.port}
    protocol: ${v.protocol}<% } %>
    '''


    def envVar = [
        PROJECT: "nameX",
        env:[
            [name:'param1', value:'value1'],
            [name:'param2', value:'value2'],
            [name:'param3', value:'value3']
        ],
        ports:[
            [protocol:'port1', port:1000],
            [protocol:'port2', port:2000]
        ]
    ]

    def xxxxx = new groovy.text.GStringTemplateEngine().createTemplate(f).make(envVar)

    println "${xxxxx}".trim()


}

def template3(){
    
    def f = new File('C:/Users/LQ/Desktop/dailyreminder.html')

    def binding = [
            "testcases":[
                ["id":"1", "name":"001-注册服务", "result":"83%", "detail":"http://172.16.3.231:8080/infces2//服务管理/服务目录/注册服务/注册服务-20210730155359"],
                ["id":"2", "name":"002-数据服务", "result":"100%", "detail":"http://172.16.3.231:8080/infces2//服务管理/服务目录/注册服务/注册服务-20210730155359"],
                ["id":"3", "name":"Z", "result":"Z", "detail":"Z"]
            ],
            "mailTitle":"邮件标题"
        
        ] 

    def engine = new groovy.text.SimpleTemplateEngine() 
    def template = engine.createTemplate(f).make(binding)
    println template.toString()

/**/
    def filePath = "C:/Users/LQ/Desktop/dailyreminder-result.html"
    PrintWriter output = new PrintWriter(new OutputStreamWriter(new FileOutputStream(new File(filePath)), "utf-8"))
    output.withPrintWriter { out -> out.print(template)}
//    new File(filePath).withPrintWriter { printWriter ->  
//       printWriter.print(template)  
//    }      

}
def x(X){
    println !X.contains("Z")
}
//x("XZ")

def xxx(){
    println "123456"[3..-1]
    println "123456".replace("1","X")

    println "${new Date().format('YYYY-MM-dd')}"    
}

//template()

//template2()

//template_foreach()


template3();